package controller

import (
	"managementsystem/common"
	"managementsystem/pkg/utils"
	"strconv"

	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/jinzhu/gorm"
)

func GinScoreStatic(ctx *gin.Context) {
	db := common.Connect()
	defer db.Close()

	var data utils.ScoreSta
	ctx.ShouldBindBodyWith(&data, binding.JSON)

	data100 := ScoreStatic100(db, data)
	data90 := ScoreStatic90(db, data)
	data80 := ScoreStatic80(db, data)
	data70 := ScoreStatic70(db, data)
	data60 := ScoreStatic60(db, data)

	//data100per := (float64(data100.Count)/float64(data100.Count+data90.Count+data80.Count+data70.Count+data60.Count))
	//data90per  := (float64(data90.Count)/float64(data100.Count+data90.Count+data80.Count+data70.Count+data60.Count))
	//data80per  := (float64(data80.Count)/float64(data100.Count+data90.Count+data80.Count+data70.Count+data60.Count))
	//data70per  := (float64(data70.Count)/float64(data100.Count+data90.Count+data80.Count+data70.Count+data60.Count))
	//data60per  := (float64(data60.Count)/float64(data100.Count+data90.Count+data80.Count+data70.Count+data60.Count))

	var score utils.ScoreFinal

	score.DataA = strconv.Itoa(data100.Count)
	score.DataB = strconv.Itoa(data90.Count)
	score.DataC = strconv.Itoa(data80.Count)
	score.DataD = strconv.Itoa(data70.Count)
	score.DataE = strconv.Itoa(data60.Count)

	ctx.ShouldBindBodyWith(&score, binding.JSON)
	ctx.JSON(200, score)
}

//100~90分
func ScoreStatic100(db *gorm.DB, data utils.ScoreSta) utils.ScoreSta {
	err := db.Table("scoresta100").Where("smajor=? OR smajor=?", data.Smajor, "通用").Where("cgrade=?", data.Cgrade).Where("cterm=?", data.Cterm).Count(&data.Count).Error
	utils.ErrCheck(err)

	return data
}

//90~80分
func ScoreStatic90(db *gorm.DB, data utils.ScoreSta) utils.ScoreSta {
	err := db.Table("scoresta90").Where("smajor=? OR smajor=?", data.Smajor, "通用").Where("cgrade=?", data.Cgrade).Where("cterm=?", data.Cterm).Count(&data.Count).Error
	utils.ErrCheck(err)

	return data
}

//80~70分
func ScoreStatic80(db *gorm.DB, data utils.ScoreSta) utils.ScoreSta {
	err := db.Table("scoresta80").Where("smajor=? OR smajor=?", data.Smajor, "通用").Where("cgrade=?", data.Cgrade).Where("cterm=?", data.Cterm).Count(&data.Count).Error
	utils.ErrCheck(err)

	return data
}

//70~60分
func ScoreStatic70(db *gorm.DB, data utils.ScoreSta) utils.ScoreSta {
	err := db.Table("scoresta70").Where("smajor=? OR smajor=?", data.Smajor, "通用").Where("cgrade=?", data.Cgrade).Where("cterm=?", data.Cterm).Count(&data.Count).Error
	utils.ErrCheck(err)

	return data
}

//60分以下
func ScoreStatic60(db *gorm.DB, data utils.ScoreSta) utils.ScoreSta {
	err := db.Table("scoresta60").Where("smajor=? OR smajor=?", data.Smajor, "通用").Where("cgrade=?", data.Cgrade).Where("cterm=?", data.Cterm).Count(&data.Count).Error
	utils.ErrCheck(err)

	return data
}
